<?php
# http://projects:8081/cdapp-2kt-api/api/journal_diversion_share.php

include_once '_portal.php';

$action = MY_POST['action'] ?? '';
$date   = MY_POST['date'] ?? null;
$data   = MY_POST['data'] ?? null;

create_diversion_table();

if ($action == '') {
  $ip   = get_client_ip();
  $list = param('SEAT_IP_LIST');
  $seat = [];
  foreach ($list as $key => $value) {
    if (in_array($ip, $value['ip'])) {
      $seat = $value['seat'];
      break;
    }
  }
  $user               = [];
  $matched            = 0;
  $seat_first_matched = '其他';
  $user_first_matched = '无';
  if (count($seat) > 0) {
    foreach ($seat as $val) {
      $sql = "SELECT * FROM `2kt_seat_status` WHERE `seatName` = '$val'";
      $res = query_parse(exec_sql($sql));
      if ($res && is_array($res) && count($res)) {
        foreach ($res as $s) {
          if (count($s['users'])) {
            foreach ($s['users'] as $u) {
              if ($u['seatPostId'] >= 2 && $u['seatRoleId'] >= 2) {
                $user[] = $u['userName'];
                if (! $matched) {
                  $seat_first_matched = $val;
                  $user_first_matched = $u['userName'];
                }

              }
            }
          }
        }
      }
    }
  }
  output([
    'ip'    => $ip,
    'seats' => $seat,
    'users' => $user,
    'seat'  => $seat_first_matched,
    'user'  => $user_first_matched,
    // 'list' => $list,
  ]);
} elseif ($action == 'query') {

  // 起始时间默认为当前时间之前两个小时
  // 截止时间默认为当前时间

  $start_time = $data['startTime'] ? date('Y-m-d H:i:s', strtotime($data['startTime'])) : date('Y-m-d H:i:s', time() - 120);
  $end_time   = $data['endTime'] ? date('Y-m-d H:i:s', strtotime($data['endTime'])) : date('Y-m-d H:i:s');

  $sql = "SELECT * FROM `2kt_journal_diversion` WHERE `origionTime`>='$start_time' AND `origionTime`<='$end_time' AND `setDisposed` = 0";

  $result = exec_sql($sql) ?? [];
  // output(['result' => $result, 'sql' => $sql]);
  output(['result' => $result]);

} elseif ($action == 'save') {
  $number                    = $data['number'] ?? null;
  $eventDate                 = $data['eventDate'] ? date('Y-m-d', strtotime($data['eventDate'])) : date('Y-m-d');
  $flightNumber              = $data['flightNumber'] ?? '';
  $departureAirport          = $data['departureAirport'] ?? '';
  $destinationAirport        = $data['destinationAirport'] ?? '';
  $diversionAirport          = $data['diversionAirport'] ?? '';
  $crewIntention             = $data['crewIntention'] ?? '';
  $eventReason               = $data['eventReason'] ?? '';
  $isEmergency               = $data['isEmergency'] ?? 0;
  $isPlannedDiversionAirport = $data['isPlannedDiversionAirport'] ?? 1;
  $aircraftType              = $data['aircraftType'] ?? '';
  $origionSeatIP             = $data['origionSeatIP'] ?? '';
  $origionSeatName           = $data['origionSeatName'] ?? '';
  $origionSeatUser           = $data['origionSeatUser'] ?? '';
  $origionTime               = $data['origionTime'] ? date('Y-m-d H:i:s', strtotime($data['origionTime'])) : date('Y-m-d H:i:s');
  // reportPlan
  $reportPlan         = $data['reportPlan'] ?? 0;
  $reportPlanSeatIP   = $data['reportPlanSeatIP'] ?? '';
  $reportPlanSeatName = $data['reportPlanSeatName'] ?? '';
  $reportPlanSeatUser = $data['reportPlanSeatUser'] ?? '';
  $reportPlanTime     = $data['reportPlanTime'] ? date('Y-m-d H:i:s', strtotime($data['reportPlanTime'])) : date('Y-m-d H:i:s');
  // reportFlow
  $reportFlow         = $data['reportFlow'] ?? 0;
  $reportFlowSeatIP   = $data['reportFlowSeatIP'] ?? '';
  $reportFlowSeatName = $data['reportFlowSeatName'] ?? '';
  $reportFlowSeatUser = $data['reportFlowSeatUser'] ?? '';
  $reportFlowTime     = $data['reportFlowTime'] ? date('Y-m-d H:i:s', strtotime($data['reportFlowTime'])) : date('Y-m-d H:i:s');
  // setDisposed
  $setDisposed         = $data['setDisposed'] ?? 0;
  $setDisposedSeatIP   = $data['setDisposedSeatIP'] ?? '';
  $setDisposedSeatName = $data['setDisposedSeatName'] ?? '';
  $setDisposedSeatUser = $data['setDisposedSeatUser'] ?? '';
  $setDisposedTime     = $data['setDisposedTime'] ? date('Y-m-d H:i:s', strtotime($data['setDisposedTime'])) : date('Y-m-d H:i:s');
  // remark
  $remark = $data['remark'] ?? '';

  $sql = '';
  if (! $number) {
    $sql = "INSERT INTO `2kt_journal_diversion`( `eventDate`, `flightNumber`, `departureAirport`, `destinationAirport`, `diversionAirport`, `crewIntention`, `eventReason`, `isEmergency`, `isPlannedDiversionAirport`, `aircraftType`, `origionSeatIP`, `origionSeatName`, `origionSeatUser`, `origionTime`, `reportPlan`, `reportPlanSeatIP`, `reportPlanSeatName`, `reportPlanSeatUser`, `reportPlanTime`, `reportFlow`, `reportFlowSeatIP`, `reportFlowSeatName`, `reportFlowSeatUser`, `reportFlowTime`, `remark`) VALUES ('{$eventDate}','{$flightNumber}','{$departureAirport}','{$destinationAirport}','{$diversionAirport}','{$crewIntention}','{$eventReason}',{$isEmergency},{$isPlannedDiversionAirport},'{$aircraftType}','{$origionSeatIP}','{$origionSeatName}','{$origionSeatUser}','{$origionTime}','{$reportPlan}','{$reportPlanSeatIP}','{$reportPlanSeatName}','{$reportPlanSeatUser}','{$reportPlanTime}','{$reportFlow}','{$reportFlowSeatIP}','{$reportFlowSeatName}','{$reportFlowSeatUser}','{$reportFlowTime}','{$remark}')";
  } else {
    $sql = "UPDATE `2kt_journal_diversion` SET `reportPlan`={$reportPlan},`reportPlanSeatIP`='{$reportPlanSeatIP}',`reportPlanSeatName`='{$reportPlanSeatName}',`reportPlanSeatUser`='{$reportPlanSeatUser}',`reportPlanTime`='{$reportPlanTime}',`reportFlow`={$reportFlow},`reportFlowSeatIP`='{$reportFlowSeatIP}',`reportFlowSeatName`='{$reportFlowSeatName}',`reportFlowSeatUser`='{$reportFlowSeatUser}',`reportFlowTime`='{$reportFlowTime}',`setDisposed`={$setDisposed},`setDisposedSeatIP`='{$setDisposedSeatIP}',`setDisposedSeatName`='{$setDisposedSeatName}',`setDisposedSeatUser`='{$setDisposedSeatUser}',`setDisposedTime`='{$setDisposedTime}',`remark`='{$reportFlowTime}' WHERE `number`={$number}";
  }
  $result = exec_sql($sql);
  // output(['result' => $result, 'sql' => $sql]);
  output(['result' => $result]);
}

die();

function create_diversion_table() {
  exec_sql("CREATE TABLE IF NOT EXISTS `2kt_journal_diversion` (
 `number` int(16) NOT NULL AUTO_INCREMENT,
 `eventDate` date NOT NULL,
 `flightNumber` varchar(16) COLLATE utf8_unicode_ci NOT NULL,
 `departureAirport` varchar(8) COLLATE utf8_unicode_ci DEFAULT NULL,
 `destinationAirport` varchar(8) COLLATE utf8_unicode_ci DEFAULT NULL,
 `diversionAirport` varchar(8) COLLATE utf8_unicode_ci DEFAULT NULL,
 `crewIntention` varchar(8) COLLATE utf8_unicode_ci DEFAULT NULL,
 `eventReason` varchar(16) COLLATE utf8_unicode_ci DEFAULT NULL,
 `isEmergency` tinyint(4) DEFAULT NULL,
 `isPlannedDiversionAirport` tinyint(4) DEFAULT NULL,
 `aircraftType` varchar(8) COLLATE utf8_unicode_ci DEFAULT NULL,
 `origionSeatIP` varchar(16) COLLATE utf8_unicode_ci DEFAULT NULL,
 `origionSeatName` varchar(8) COLLATE utf8_unicode_ci DEFAULT NULL,
 `origionSeatUser` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
 `origionTime` datetime DEFAULT NULL,
 `reportPlan` tinyint(4) DEFAULT NULL,
 `reportPlanSeatIP` varchar(16) COLLATE utf8_unicode_ci DEFAULT NULL,
 `reportPlanSeatName` varchar(8) COLLATE utf8_unicode_ci DEFAULT NULL,
 `reportPlanSeatUser` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
 `reportPlanTime` datetime DEFAULT NULL,
 `reportFlow` tinyint(4) DEFAULT NULL,
 `reportFlowSeatIP` varchar(16) COLLATE utf8_unicode_ci DEFAULT NULL,
 `reportFlowSeatName` varchar(8) COLLATE utf8_unicode_ci DEFAULT NULL,
 `reportFlowSeatUser` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
 `reportFlowTime` datetime DEFAULT NULL,
 `setDisposed` tinyint(4) DEFAULT 0,
 `setDisposedSeatIP` varchar(16) COLLATE utf8_unicode_ci DEFAULT NULL,
 `setDisposedSeatName` varchar(8) COLLATE utf8_unicode_ci DEFAULT NULL,
 `setDisposedSeatUser` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
 `setDisposedTime` datetime DEFAULT NULL,
 `remark` text COLLATE utf8_unicode_ci NOT NULL,
 `createTime` datetime DEFAULT CURRENT_TIMESTAMP,
 `lastUpdateTime` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 PRIMARY KEY (`number`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci");
}